import coverRightPng from '@/assets/cover-right.png';
import { RootState } from '@/models/index';
import { RootStackParamList } from '@/navigator/index';
import { BlurView } from '@react-native-community/blur';
import { RouteProp } from '@react-navigation/native';
import { useHeaderHeight } from '@react-navigation/stack';
import React, { Component } from 'react';
import { Animated, Image, StyleSheet, Text, View } from 'react-native';
import { PanGestureHandler, PanGestureHandlerStateChangeEvent, State } from 'react-native-gesture-handler';
import { connect, ConnectedProps } from 'react-redux';
import Tab from './Tab';

const mapStateToProps = ({ album }: RootState) => {
  return {
    summary: album.summary,
    author: album.author
  }
}

const connector = connect(mapStateToProps);
type ModelState = ConnectedProps<typeof connector>;

interface IProps extends ModelState {
  headerHeight: number;
  route: RouteProp<RootStackParamList, 'Album'>;
}

const USE_NATIVE_DRIVER = true;

class Album extends Component<IProps> {
  translateY = new Animated.Value(0);
  componentDidMount() {
    const { dispatch, route } = this.props;
    const id = route.params.item.id;
    dispatch({
      type: 'album/fetchAlbum',
      payload: {
        id: id
      }
    });

  }

  onGestureEvent = Animated.event([{ nativeEvent: { translationY: this.translateY } }],
    { useNativeDriver: USE_NATIVE_DRIVER }
  )

  onHandlerStateChange = ({nativeEvent}: PanGestureHandlerStateChangeEvent) => {
    if (nativeEvent.oldState == State.ACTIVE) {
      let {translationY} = nativeEvent;
    }
  }

  renderHeader = () => {
    const { headerHeight, summary, author, route } = this.props;
    const { title, image } = route.params.item;
    return (
      <View style={[styles.header, { paddingTop: headerHeight }]}>
        <Image source={{ uri: image }} style={styles.background} />
        <BlurView blurAmount={3} blurType="light" style={StyleSheet.absoluteFillObject} />
        <View style={styles.leftView}>
          <Image source={{ uri: image }} style={styles.thumbnail} />
          <Image source={coverRightPng} style={styles.coverRight} />
        </View>
        <View style={styles.rightView}>
          <Text style={styles.title}>{title}</Text>
          <View style={styles.summary}>
            <Text numberOfLines={1} style={styles.summaryText}>{summary}</Text>
          </View>
          <View>
            <Image source={{ uri: author.avatar }} style={styles.avatar} />
            <Text style={styles.name}>{author.name}</Text>
          </View>
        </View>
      </View>
    )
  }

  render() {
    return (
      <PanGestureHandler onGestureEvent={this.onGestureEvent}
        onHandlerStateChange={this.onHandlerStateChange}
      >
        <Animated.View style={[
          styles.container,
          {
            transform: [{ translateY: this.translateY }],
          }
        ]}>
          {this.renderHeader()}

          <Tab />
        </Animated.View>
      </PanGestureHandler>
    )
  }
}

function Wapper(props: IProps) {
  const tmpHeaderHeight = useHeaderHeight();
  return <Album  {...props} headerHeight={tmpHeaderHeight} />
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  header: {
    height: 260,
    flexDirection: 'row',
    paddingHorizontal: 20,
    alignItems: 'center',
  },
  background: {
    ...StyleSheet.absoluteFillObject,
    backgroundColor: '#eee'
  },
  leftView: {
    marginRight: 26,
  },
  thumbnail: {
    width: 98,
    height: 98,
    borderColor: '#fff',
    borderWidth: StyleSheet.hairlineWidth,
    borderRadius: 8,
    backgroundColor: '#fff',
  },
  coverRight: {
    height: 98,
    position: 'absolute',
    right: -23,
    resizeMode: 'contain',
  },
  title: {
    color: '#fff',
    fontSize: 18,
    fontWeight: '900',
  },
  rightView: {
    flex: 1,
  },
  summary: {
    backgroundColor: 'rgba(0, 0, 0, 0.3)',
    padding: 10,
    marginVertical: 10,
    borderRadius: 4,

  },
  summaryText: {
    color: '#fff',
  },
  author: {
    flexDirection: 'row',
    alignItems: 'center'
  },
  name: {
    color: '#fff',

  },
  avatar: {
    height: 26,
    width: 26,
    borderRadius: 13,
    marginRight: 8,
  }
})
export default connector(Wapper);
